home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / findfile.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  1KB  |  63 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3. #include <stat.h>
  4.  
  5. static char *_findext(p, q, ext)
  6.     register char *p, *q, *ext;
  7.     {
  8.     if(ext == NULL)
  9.         ext = ".";
  10.     while(p < q)
  11.         {
  12.         if(!stricmp(p, ext))
  13.             return(p);
  14.         while(*p++)
  15.             ;
  16.         }
  17.     return(NULL);
  18.     }
  19.  
  20. char *findfile(afn, ext)
  21.     char *afn, *ext;
  22.     {
  23.     char *fullpath(), *strrchr(), *strchr(), *strcpy();
  24.     register char *p, *q, *e;
  25.     struct stat dta, *pdta;
  26.  
  27.     afn = fullpath(NULL, afn);
  28.     if((p = strrchr(afn, '\\')) &&
  29.        (p = strchr(p, '.')))        /* .EXT specified */
  30.         return(access(afn, 0x00) ? afn : NULL);
  31.     /*
  32.      * No extension specified, search directory for any extension
  33.      * and try to match with the list.
  34.      */
  35.     p = strrchr(afn, '\0');
  36.     p[0] = '.';
  37.     p[1] = '*';
  38.     p[2] = '\0';
  39.     q = ext;
  40.     while(*q)                /* q = end of exts / match */
  41.         while(*q++)
  42.             ;
  43.     pdta = ((struct stat *) Fgetdta());
  44.     Fsetdta(&dta);
  45.     if(Fsfirst(afn, 0x00) == 0)
  46.         {
  47.         do
  48.             {
  49.             e = strchr(dta.st_name, '.');
  50.             if(e = _findext(ext, q, e))
  51.                 q = e;
  52.             }
  53.             while(Fsnext() == 0);
  54.         }
  55.     Fsetdta(pdta);
  56.     if(*q)
  57.         {
  58.         strcpy(p, q);
  59.         return(afn);
  60.         }
  61.     return(NULL);
  62.     }
  63.